<template>
    <el-dialog :title="!dataForm.pkModule ? '新增' : '修改'" :close-on-click-modal="false" :before-close="closeDailog"
        :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="模块名" prop="moduleName">
                <el-input v-model="dataForm.moduleName" placeholder="模块名" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="模块代码" prop="moduleCode">
                <el-input v-model="dataForm.moduleCode" placeholder="模块代码" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="模块描述" prop="moduleDesc">
                <el-input v-model="dataForm.moduleDesc" placeholder="模块描述" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="数据库名称" prop="databaseName">
                <el-input v-model="dataForm.databaseName" placeholder="数据库名称" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="数据库地址" prop="databaseAddress">
                <el-input v-model="dataForm.databaseAddress" placeholder="数据库地址" maxlength="30" show-word-limit>
                </el-input>
            </el-form-item>
            <el-form-item label="数据库账号" prop="databaseAccount">
                <el-input v-model="dataForm.databaseAccount" placeholder="数据库账号" maxlength="30" show-word-limit>
                </el-input>
            </el-form-item>
            <el-form-item label="数据库密码" prop="databasePassword">
                <el-input v-model="dataForm.databasePassword" placeholder="数据库密码" maxlength="30" show-word-limit>
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog()">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as centerService from '@/api/development/center'

    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkModule: 0,
                    moduleName: '',
                    moduleCode: '',
                    moduleDesc: '',
                    databaseName: '',
                    databaseAddress: '',
                    databaseAccount: '',
                    databasePassword: '',
                    revision: '',
                },
                dataRule: {
                    moduleName: [{
                        required: true,
                        message: '模块名不能为空',
                        trigger: 'blur'
                    }],
                    moduleCode: [{
                        required: true,
                        message: '模块代码不能为空',
                        trigger: 'blur'
                    }],
                    moduleDesc: [{
                        required: true,
                        message: '模块描述不能为空',
                        trigger: 'blur'
                    }],
                    databaseName: [{
                        required: true,
                        message: '数据库名称不能为空',
                        trigger: 'blur'
                    }],
                    databaseAddress: [{
                        required: true,
                        message: '数据库地址不能为空',
                        trigger: 'blur'
                    }],
                    databaseAccount: [{
                        required: true,
                        message: '数据库账号不能为空',
                        trigger: 'blur'
                    }],
                    databasePassword: [{
                        required: true,
                        message: '数据库密码不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkModule = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkModule) {
                        centerService.getCenterInfo(this.dataForm.pkModule).then(data => {
                            this.dataForm.moduleName = data.moduleName
                            this.dataForm.moduleCode = data.moduleCode
                            this.dataForm.moduleDesc = data.moduleDesc
                            this.dataForm.databaseName = data.databaseName
                            this.dataForm.databaseAddress = data.databaseAddress
                            this.dataForm.databaseAccount = data.databaseAccount
                            this.dataForm.databasePassword = data.databasePassword
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkModule': this.dataForm.pkModule || undefined,
                            'moduleName': this.dataForm.moduleName,
                            'moduleCode': this.dataForm.moduleCode,
                            'moduleDesc': this.dataForm.moduleDesc,
                            'databaseName': this.dataForm.databaseName,
                            'databaseAddress': this.dataForm.databaseAddress,
                            'databaseAccount': this.dataForm.databaseAccount,
                            'databasePassword': this.dataForm.databasePassword,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkModule ? centerService.addCenter(params) : centerService
                            .updateCenter(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            }
        }
    }
</script>